import {
    poiLocation, poiQuery,LBSLocation
} from "../../../../api/amap";
const util = require('../../../../utils/util.js')
var app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        center: [],
        lng: 118.1,
        lat: 24.46,
        point: {
            position: [],
            title: "This is a marker",
        },
        addressList: [],
        selectAddress: "",
        addressType: false,
        building:'',
        currentLocation:"",
        selectAddressTwo:''
    },
    async onLoad (options) {
        await this.repositionClick()
    },
    getBuilding() {
        let _this = this;
        let data = {
            lng: this.data.lng,
            lat: this.data.lat
        };
        poiLocation(data)
            .then((res) => {
                let addressList = [];
                if (res.type == "success") {
                    if (res.data.pois.length > 0) {
                        res.data.pois.map((item) => {
                            item.checked = false
                        })
                        this.data.addressList = res.data.pois;
                        this.setData({
                            addressList:this.data.addressList
                        })
                    } else {
                        this.data.addressList = []
                        this.setData({
                            addressList:this.data.addressList
                        })
                    }
                }
            })
    },
    getAddress(callback) {
        let _this = this;
        let data = {lat: this.data.lat, lng: this.data.lng, keyword: this.data.addressName};
        poiQuery(data)
            .then((res) => {
                let addressList = [];
                if (res.type == "success") {
                    if (res.data.pois.length > 0) {
                        this.data.addressList = res.data.pois;
                        this.setData({
                            addressList: this.data.addressList
                        })
                    } else {
                        this.data.addressList = []
                        this.setData({
                            addressList: this.data.addressList
                        })
                    }
                    if (callback) {
                        callback()
                    }
                }
            })
    },
    dragend(e) {
        console.info("用户拖动地图时触发该事件" + JSON.stringify(e.center));
        let _this = this;
        this.data.lng = e.center.longitude;
        this.data.lat = e.center.latitude;
        if (this.data.addressType == false){
            _this.getBuilding();
        }
    },
    inputClick(e){
        this.data.addressName = e.detail.value
        this.setData({
            addressName:this.data.addressName
        })
    },
    submit() {
        if (util.isNull(this.data.selectAddress)) {
            wx.showToast({
                icon: "none",
                title: '请点击选择地址',
                duration: 5000
            })
        } else {
            wx.navigateBack({
                delta: 1
            })
            app.event.emit("onCartChange",this.data.selectAddress);
        }
    },
    addressClick(data) {
        let center = []
        center[0] = data.currentTarget.dataset.item.location.split(",")[0]
        center[1] = data.currentTarget.dataset.item.location.split(",")[1]
        this.data.center = center
        if (this.data.addressList.length > 0) {
            this.data.addressList.map((item) => {
                if (item.id == data.currentTarget.dataset.item.id) {
                    item.checked = true
                    this.data.selectAddress = item
                    this.data.addressType = true
                    setTimeout( () => {
                        this.data.addressType = false
                    },3000)
                } else {
                    item.checked = false
                }
            })
        }
        this.setData({
            addressList:this.data.addressList
        })

    },
    searchSubmit() {
        let _this = this
        if (util.isNull(this.data.addressName)) {
            wx.showToast({
                icon: "none",
                title: '请输入搜索地点',
                duration: 5000
            })
        } else {
            this.getAddress()
        }
    },
    repositionClick(){
        // wx.getLocation({
        //     type: 'wgs84',
        //     geocode:true,//设置该参数为true可直接获取经纬度及城市信息
        //     success: res=>{
        //         this.data.lng =  res.longitude
        //         this.data.lat =  res.latitude
        //         // this.addressName = ""
        //         this.LBSLocation()
        //     },
        //     fail:err=>{
        //         wx.showToast({
        //             title: err,
        //             icon:'none'
        //         });
        //     }
        // })
    },
    LBSLocation(){
        let params = {
            lng: this.data.lng,
            lat: this.data.lat
        }
        LBSLocation(params).then(res=>{
            console.log(res ,"LBSLocationLBSLocationLBSLocation")

            this.data.building = res.data.adName + res.data.address + "(" + res.data.name + ")"
            this.data.currentLocation = res.data.address
            this.data.selectAddressTwo = res.data
            this.setData({
                building:this.data.building,
                currentLocation:this.data.currentLocation,
                selectAddressTwo:this.data.selectAddressTwo
            })
            this.getBuilding()
        })
    },
    selectCurrentLocation(e){
        this.data.selectAddressTwo.adCode = this.data.selectAddressTwo.areaId
        this.data.selectAddressTwo.adName = this.data.selectAddressTwo.areaName
        this.data.selectAddressTwo.location =  this.data.lng+ "," +this.data.lat
        app.event.emit("onCartChange",this.data.selectAddressTwo)
        wx.navigateBack({
            delta: 1
        })
    },
})
